package ld.oauth.blog.controller.article;

import com.ld.anotation.Log;
import com.ld.common.ResultInfoFactory;
import ld.oauth.blog.model.bo.ArticleBo;
import ld.oauth.blog.model.bo.CommentBo;
import ld.oauth.blog.model.po.article.Abvert;
import ld.oauth.blog.model.po.article.Category;
import ld.oauth.blog.model.qo.ArticleQo;
import ld.oauth.blog.model.vo.ArticleDetail;
import ld.oauth.blog.model.vo.ArticleVo;
import ld.oauth.blog.model.vo.CategoryLabelVo;
import ld.oauth.blog.model.vo.CommentVo;
import ld.oauth.blog.service.article.AbvertService;
import ld.oauth.blog.service.article.ArticleService;
import ld.oauth.blog.service.article.CategoryService;
import ld.oauth.blog.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ld
 * @since 2022-09-24
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AbvertService abvertService;


    /**
     * 获取文章分类列表
     *
     * @return
     */
    @RequestMapping("/category/list")
    public ResultInfoFactory<List<Category>> list() {
        return ResultInfoFactory.success(categoryService.getList());
    }

    /**
     * 获取所有分类列表下的所有标签
     *
     * @return
     */
    @RequestMapping("/category/categoryLabelVoList")
    @Log(module = "文章模块",description = "获取所有分类列表下的所有标签")
    public ResultInfoFactory<List<CategoryLabelVo>> categoryLabelVoList() {
        List<CategoryLabelVo> list = categoryService.categoryLabelVoList();
        return ResultInfoFactory.success(list);
    }

    /**
     * 新增或修改文章
     *
     * @param articleBo
     * @return
     */
    @PostMapping("/article/insertOrUpdate")
    @Log(module = "文章模块",description = "新增或修改文章")
    public ResultInfoFactory<Integer> insertOrUpdate(@RequestBody ArticleBo articleBo) {
        return ResultInfoFactory.success(articleService.saveOrUpdate(articleBo));
    }

    /**
     * 分页条件查询文章列表信息
     *
     * @return
     */
    @PostMapping("/article/pageQuery")
    @Log(module = "文章模块",description = "分页条件查询文章列表信息")
    public ResultInfoFactory<PageResult<ArticleVo>> pageQuery(@RequestBody ArticleQo articleQo) {
        return ResultInfoFactory.success(articleService.pageQuery(articleQo));
    }

    /**
     * 根据文章id查询文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("/article/articleDetailById/{id}")
    @Log(module = "文章模块",description = "根据文章id查询文章详情")
    public ResultInfoFactory<ArticleDetail> articleDetailById(@PathVariable("id") Integer id) {
        return ResultInfoFactory.success(articleService.articleDetailById(id));
    }

    /**
     * 首页轮播图查询list
     *
     * @return
     */
    @GetMapping("/article/abvertList/{id}")
    @Log(module = "文章模块",description = "首页轮播图查询list")
    public ResultInfoFactory<List<Abvert>> abvertList(@PathVariable("id") Integer id) {
        return ResultInfoFactory.success(abvertService.abvertList(id));
    }

    /**
     * 更新文章浏览数
     *
     * @param id
     * @return
     */
    @RequestMapping("/article/updateArticleViewCountById/{id}")
    @Log(module = "文章模块",description = "更新文章浏览数")
    public ResultInfoFactory<String> updateArticleViewCountById(@PathVariable("id") Integer id) {
        articleService.updateArticleViewCountById(id);
        return ResultInfoFactory.success();
    }


    /**
     * 更新文章点赞数
     *
     * @param articleId
     * @param count
     * @return
     */
    @RequestMapping("/article/updateArticleThumbById/{articleId}/{count}")
    @Log(module = "文章模块",description = "更新文章点赞数")
    public ResultInfoFactory<String> updateArticleThumbById(@PathVariable("articleId") Integer articleId, @PathVariable("count") Integer count) {
        articleService.updateArticleThumbById(articleId, count);
        return ResultInfoFactory.success();
    }

    /**
     * 根据文章id获取评论list
     *
     * @param articleId
     * @return
     */
    @RequestMapping("/article/commentListByArticleId/{articleId}")
    @Log(module = "文章模块",description = "根据文章id获取评论list")
    public ResultInfoFactory<List<CommentVo>> commentListByArticleId(@PathVariable("articleId") Integer articleId) {
        return ResultInfoFactory.success(articleService.commentListByArticleId(articleId));
    }

    /**
     * 新增文章评论
     *
     * @param commentBo
     * @return
     */
    @PostMapping("/article/insertComment")
    @Log(module = "文章模块",description = "新增文章评论")
    public ResultInfoFactory insertComment(@RequestBody CommentBo commentBo) {
        return articleService.insertComment(commentBo) == true ? ResultInfoFactory.success() : ResultInfoFactory.failed();
    }

    /**
     * 根据id删除评论
     * @param id
     * @return
     */
    @GetMapping("/article/deleteCommentById/{id}")
    @Log(module = "文章模块",description = "根据id删除评论")
    public ResultInfoFactory deleteCommentById(@PathVariable("id") Integer id) {
        return articleService.removeById(id) == true ? ResultInfoFactory.success() : ResultInfoFactory.failed();
    }

    /**
     * 根据标签id分页查询文章列表
     * @param articleQo
     * @return
     */
    @PostMapping("/pageArticleByLabelId")
    @Log(module = "文章模块",description = "根据标签id分页查询文章列表")
    public ResultInfoFactory<PageResult<ArticleDetail>> queryArticleDetailByLabelId(@RequestBody ArticleQo articleQo){
        return ResultInfoFactory.success(articleService.queryArticleDetailByLabelId(articleQo));
    }


}
